package com.web.servlet;

import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web.constant.Constant;
import com.web.po.Huodong;
import com.web.po.Penson;
import com.web.vo.ResultVO;

@WebServlet("/yao/*")
public class YaoServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public ServletContext app = null;
	
	@Override
	public boolean before(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		app = request.getServletContext();
		return true;
	}

	public ResultVO addPenson(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		String name = (String)data.get("name");
		
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		if (null==huodongs) {
			res.setMessage("当前无活动");
			res.setStatus(Constant.ERROR_STATUS);
			return res;
		}
		Huodong temphuodong = null;
		String h_id = (String)data.get("h_id");
		Iterator<Huodong> item = huodongs.iterator();
		while (item.hasNext()) {
			Huodong huodong = item.next();
			if (huodong.getH_id().equals(h_id)) {
				temphuodong = huodong;
				item.remove();
				break;
			}
		}
		if (null==temphuodong) {
			res.setMessage("没有该活动");
			res.setStatus(Constant.ERROR_STATUS);
			return res;
		}
		
		Vector<Penson> pensons = temphuodong.getPensons();
		boolean pensonIsHasName = false;
		Iterator<Penson> pIterator = pensons.iterator();
		while (pIterator.hasNext()) {
			Penson p = pIterator.next();
			//是否重名
			if (p.getName().equals(name)) {
				pensonIsHasName = true;
				break;
			}
		}
		
		if (!pensonIsHasName) {
			Penson penson = new Penson();
			penson.setP_id(UUID.randomUUID().toString());
			penson.setCount(0);
			penson.setName(name);
			
			pensons.add(penson);
			temphuodong.setPensons(pensons);
			res.setStatus(Constant.SUCCESS_STATUS);
			res.setMessage("添加成功");
			res.setData(penson);
		} else {
			res.setStatus(Constant.ERROR_STATUS);
			res.setMessage("该名称已存在");
		}
		
		huodongs.add(temphuodong);
		synchronized (app) {
			app.setAttribute(Constant.AppHuodongName, huodongs);
		}
		
		return res;
	}

	public ResultVO addPensonCount(HttpServletRequest request, HttpServletResponse response, Map<String, Object> data) {
		ResultVO res = new ResultVO();
		String p_id = (String)data.get("p_id");
		
		Vector<Huodong> huodongs = (Vector<Huodong>)app.getAttribute(Constant.AppHuodongName);
		if (null==huodongs) {
			res.setMessage("当前无活动");
			res.setStatus(Constant.ERROR_STATUS);
			return res;
		}
		Huodong temphuodong = null;
		String h_id = (String)data.get("h_id");
		Iterator<Huodong> item = huodongs.iterator();
		while (item.hasNext()) {
			Huodong huodong = item.next();
			if (huodong.getH_id().equals(h_id)) {
				temphuodong = huodong;
				item.remove();
				break;
			}
		}
		if (null==temphuodong) {
			res.setMessage("没有该活动");
			res.setStatus(Constant.ERROR_STATUS);
			return res;
		}
		
		if (!temphuodong.getIsbegin()) {
			res.setMessage("活动未开始");
			res.setStatus(Constant.ERROR_STATUS);
			huodongs.add(temphuodong);
			synchronized (app) {
				app.setAttribute(Constant.AppHuodongName, huodongs);
			}
			return res;
		}
		
		Vector<Penson> pensons = temphuodong.getPensons();
		Penson penson = null;
		Iterator<Penson> pIterator = pensons.iterator();
		while (pIterator.hasNext()) {
			Penson p = pIterator.next();
			if (p.getP_id().equals(p_id)) {
				penson = p;
				pIterator.remove();
				break;
			}
		}
		
		if (null!=penson) {
			penson.setCount(penson.getCount()+1);
			pensons.add(penson);
			temphuodong.setPensons(pensons);
			res.setStatus(Constant.SUCCESS_STATUS);
			res.setMessage("添加成功");
			res.setData(penson);
		} else {
			res.setStatus(Constant.ERROR_STATUS);
			res.setMessage("该玩家不存在");
		}
		huodongs.add(temphuodong);
		synchronized (app) {
			app.setAttribute(Constant.AppHuodongName, huodongs);
		}
		
		return res;
	}
	
}
